#!/usr/bin/env python
# -*- coding: utf-8 -*-

from .models import Model


class Env(object):
    def __init__(self, rpc, models=None):
        self.rpc = rpc
        if not models:
            models = []

        env = dict((model._name, model) for model in models)
        print('Env:', env)
        self._env = env

    @property
    def version(self):
        return self.rpc.version

    @property
    def sid(self):
        return self.rpc.sid

    @property
    def session(self):
        return self.rpc.session

    def __getitem__(self, index):
        # print('env  get item', index)
        # print('env  get item ,2', self._env)
        # print('env  get item, 3', self._env.get(index))

        MyModel = self._env.get(index) or Model
        # print('env  get item, 4', Model)
        # print('env  get item, 5', MyModel)
        return MyModel(self, index)

    def ref2(self, xmlid):
        res = self['ir.model.data'].xmlid_to_res_model_res_id(xmlid, True)
        return res

    def ref(self, xmlid):
        res = self['ir.model.data'].xmlid_to_res_model_res_id(xmlid, True)
        return res[1]
